﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Golos.Polls.PollCache
{
    public partial class Cache
    {
        public PollInfo GetPollByQuestionId(Guid questionId)
        {
            lock (lockcache)
            {
                if (!questionPolls.ContainsKey(questionId) || questionPolls[questionId].IsDeleted)
                    return null;
                return questionPolls[questionId];
            }
        }

        public UserAnswerInfo CheckUserAnswer(Guid questionId, Guid userId)
        {
            lock (lockcache)
            {
                if (!questionUserAnswers.ContainsKey(questionId))
                    throw new Exception(string.Format("Can't find QuestionId = {0}", questionId));
                return !questionUserAnswers[questionId].ContainsKey(userId) ? null : questionUserAnswers[questionId][userId];
            }
        }

        public Dictionary<Guid, int> GetVoicesCount(Guid questionId)
        {
            lock (lockcache)
            {
                if (!voicesCountCache.ContainsKey(questionId))
                    throw new Exception(string.Format("Can't find QuestionId = {0}", questionId));
                return voicesCountCache[questionId];
            }
        }

        public List<Guid> GetUserQuestions(Guid userId)
        {
            lock (lockcache)
            {
                return userOwnerQuestions.ContainsKey(userId) ? userOwnerQuestions[userId] : new List<Guid>();
            }
        }

        public List<Guid> GetVotedQuestions(Guid userId)
        {
            lock (lockcache)
            {
                return userVotedQuestions.ContainsKey(userId) ? userVotedQuestions[userId] : new List<Guid>();
            }
        }
    }
}
